package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.efounder.chat.activity.BaseActivity;
import com.efounder.chat.view.CommonOtherPopWindow;
import com.efounder.util.EnvSupportManager;
import com.efounder.utils.ResStringUtil;
import com.pansoft.nfc.mifare.Common;
import com.pansoft.nfc.mifare.activity.MifareAddCardActivity;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.adapter.NewCardStackAdapter;
import com.pansoft.openplanet.bean.CardBean;
import com.pansoft.openplanet.db.CardDao;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

import java.util.ArrayList;
import java.util.List;


/**
 * 卡片列表界面
 *
 * @author maming
 * @date 2019/4/23 17:16
 */
public class NewStackActivity extends BaseActivity implements View.OnClickListener {
    //
    private TextView tv_title,tv_what_is_card;
    private ImageView iv_add;
    private RecyclerView recyclerView;
    //
    private NewCardStackAdapter mCardStackAdapter;
    private List<CardBean> cardBeanList;

    public static void start(Context context) {
        Intent starter = new Intent(context, NewStackActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assets_activity_card_list_new_stack);
        initView();
        initData();
        initListener();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        tv_what_is_card = (TextView) findViewById(R.id.tv_what_is_card);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        tv_title.setText(getString(R.string.open_planet_stack_title));
        iv_add.setVisibility(View.VISIBLE);
        iv_add.setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(NewStackActivity.this, LinearLayoutManager.VERTICAL, false));
        mCardStackAdapter = new NewCardStackAdapter(R.layout.item_new_list_card);

        recyclerView.setAdapter(mCardStackAdapter);
        setAdapterData();
        //添加底部布局
        addCardFootLayout();
    }

    /**
     * 添加列表底部布局
     */
    private void addCardFootLayout() {
        View view = LayoutInflater.from(this).inflate(R.layout.assets_item_new_stack_foot_layout,null);
        //底部item点击事件  添加卡片
        view.findViewById(R.id.cl_add_card).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 直接读取NFC
                if (!EnvSupportManager.isSupportNfcWallet() && !EnvSupportManager.isSupportForceBleNfc()) {
                    return;
                }
                if (!EnvSupportManager.isSupportForceBleNfc() && Common.hasMifareClassicSupport(NewStackActivity.this)) {
                    MifareAddCardActivity.start(NewStackActivity.this);
                    //蓝牙读卡器
                } else {
                    BleBlutoothScanActivity.start(NewStackActivity.this,
                            BleBlutoothScanActivity.TYPE_ADD_CARD, BleBlutoothScanActivity.BLE_TYPE_NFC);
                }
            }
        });
        //购买卡片点击事件
        view.findViewById(R.id.ll_buy_card).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2019/4/30 购买卡片跳转
//                ToastUtils.showShort("购买卡片点击事件");
            }
        });
        mCardStackAdapter.addFooterView(view);
    }

    /**
     * 设置适配器的模拟数据
     */
    private void setAdapterData() {
        cardBeanList = new ArrayList<>();
        Observable.create(new ObservableOnSubscribe<List<CardBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<CardBean>> emitter) throws Exception {
                emitter.onNext(new CardDao(NewStackActivity.this).getCardBeanList());
            }
        }).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {

            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<CardBean>>() {
                    @Override
                    public void accept(List<CardBean> cardBeans) throws Exception {
                        cardBeanList.addAll(cardBeans);
                        mCardStackAdapter.setNewData(cardBeanList);
                    }
                });
    }

    /**
     * 初始化点击事件
     */
    private void initListener() {
        //点击查看按钮跳转到卡片详情界面
        mCardStackAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                NewAssetCardsDetailsActivity.start(NewStackActivity.this, cardBeanList.get(position).getUid());
            }
        });
        //什么是卡片的点击事件
        tv_what_is_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2019/4/30 什么是卡片的点击事件
//                ToastUtils.showShort("什么是卡片的点击事件");
            }
        });
    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View view) {
        showPopWindow();
    }

    /**
     * 添加卡片的菜单
     */
    private void showPopWindow() {
        final CommonOtherPopWindow commonOtherPopWindow = new CommonOtherPopWindow(NewStackActivity.this);
        commonOtherPopWindow.addMenuButton(ResStringUtil.getString(R.string.open_planet_add_cards), 0)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 直接读取NFC
                        if (!EnvSupportManager.isSupportNfcWallet() && !EnvSupportManager.isSupportForceBleNfc()) {
                            return;
                        }
                        if (!EnvSupportManager.isSupportForceBleNfc() && Common.hasMifareClassicSupport(NewStackActivity.this)) {
                            MifareAddCardActivity.start(NewStackActivity.this);
                            //蓝牙读卡器
                        } else {
                            BleBlutoothScanActivity.start(NewStackActivity.this,
                                    BleBlutoothScanActivity.TYPE_ADD_CARD, BleBlutoothScanActivity.BLE_TYPE_NFC);
                        }
                        commonOtherPopWindow.dismiss();
                    }
                });
        commonOtherPopWindow.showPopupWindow(iv_add);
    }
}

